package com.chen.web_chat.handler;

import com.chen.web_chat.entity.vo.Result;
import com.chen.web_chat.execption.MyException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {
    //处理器会按照定义的顺序执行，如果捕捉Exception将永远无法捕获自定义的异常
    //自定义异常处理
    @ExceptionHandler(MyException.class)
    @ResponseBody
    public Result<String> error(MyException e) {
        if (e.getCode() == 555){
            // 自定义错误信息异常
            return Result.build(null, e.getCode(), e.getMessage());
        }
        // 枚举错误异常
        return Result.error(e.getMessage(), e.getResultCodeEnum());
    }
    //全局異处理器
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result<String> error() {
        return Result.build("error",555, "发生未知错误");
    }

}
